package com.pro.springbootshop.service.impl;

import com.pro.springbootshop.entity.HistoryOrder;
import com.pro.springbootshop.entityExtend.HistoryOrderReturn;
import com.pro.springbootshop.entityExtend.SearchOrder;
import com.pro.springbootshop.mapper.HistoryOrderMapper;
import com.pro.springbootshop.service.HistoryOrderService;
import com.pro.springbootshop.util.SellerTokenUtil;
import com.pro.springbootshop.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class HistoryOrderServiceImpl implements HistoryOrderService {
    @Autowired
    TokenUtil tokenUtil;
    @Autowired
    SellerTokenUtil sellerTokenUtil;
    public HistoryOrderMapper historyOrderMapper;
    @Autowired
    public void setUserMapper (HistoryOrderMapper historyOrderMapper) {
        this.historyOrderMapper= historyOrderMapper;
    }
    @Override
    public HistoryOrderReturn getAllHistoryOrderList(SearchOrder searchOrder,String token) {
        int page=searchOrder.getPage();
        int limit=searchOrder.getLimit();
        String buyerId=searchOrder.getBuyerId();
        int pageNo=(page-1)*limit;
        Map<String,String> sellerMap=sellerTokenUtil.parseToken(token);
        String sellerId=sellerMap.get("sellerId");
        int total=historyOrderMapper.sellerGetHistoryOrderCounts(sellerId,"admin").size();
        List<HistoryOrder> historyList;
        if(buyerId.equals("")){
            historyList= historyOrderMapper.sellerGetHistoryOrder(pageNo,limit,sellerId,"admin");
        }else {
            historyList= historyOrderMapper.getHistoryOrderByBuyerId(pageNo,limit,buyerId,sellerId,"admin");
            total=historyOrderMapper.getHistoryOrderCountsByBuyerId(buyerId,sellerId,"admin").size();
        }
        for(HistoryOrder h:historyList){
            h.getWares().setPictureList(h.getWares().getWaresPicture().split(";"));
        }
        return new HistoryOrderReturn(historyList,total);
    }

    @Override
    public String deleteHistoryOrder(String historyOrderId) {
        int i =historyOrderMapper.deleteById(historyOrderId);
        if(i>0){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }

    @Override
    public HistoryOrderReturn getHistoryOrderList(Integer page, Integer limit, String token) {
        Map<String,String> buyerMap=tokenUtil.parseToken(token);
        String buyerId=buyerMap.get("buyerId");
        int pageNo=(page-1)*limit;
        int total=historyOrderMapper.getHistoryOrderCountsByBuyerId(buyerId,"admin","admin").size();
        List<HistoryOrder> historyOrderList= historyOrderMapper.getHistoryOrderByBuyerId(pageNo,limit,buyerId,"admin","admin");
        for(HistoryOrder h:historyOrderList){
            h.getWares().setPictureList(h.getWares().getWaresPicture().split(";"));
        }
        return new HistoryOrderReturn(historyOrderList,total);
    }

    @Override
    public List<HistoryOrder> getSuccessHistoryOrder(String token) {
        Map<String,String> buyerMap=tokenUtil.parseToken(token);
        String buyerId=buyerMap.get("buyerId");
        return historyOrderMapper.getHistoryOrderForAfterSales(buyerId);
    }
}
